from django.shortcuts import render

# Create your views here.
from .models import User,CateNew,News,Teacher,Student,Stars
from .ser import NewSweializer,TeacherSweializer,SutendsSweializer
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.paginator import Paginator

class Login(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        print(name,password)
        b = User.objects.get(username=name,password=password)
        if b:
            return Response({'msg':'登录成功','code':200})
        else:
            return Response({'msg': '登录失败', 'code': 200})

class Newshow(APIView):
    def get(self,request):
        data = request.query_params.get('qwe')
        news = News.objects.all()
        page = Paginator(news,1)
        page_list = [i for i in page.page_range]
        page_page = page.page(data)
        page_num = page.num_pages
        new = NewSweializer(page_page,many=True)
        return Response({'new_list':new.data,'page_list':page_list,'page_num':page_num})
class GetNew(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        new_info = News.objects.get(id=id)
        new_ser = NewSweializer(new_info)
        print(new_ser.data)
        return Response(new_ser.data)

class Starsad(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        new = News.objects.get(id=id)
        new.stars+=1
        News.objects.update(stars=new.stars)
        return Response({'msg':'点赞成功',"code":200})


class LoginT(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        print(name, password)
        b = Teacher.objects.get(name=name, password=password)
        if b:
            return Response({'msg': '登录成功', 'code': 200,'id':b.id})
        else:
            return Response({'msg': '登录失败', 'code': 200})

class StudentView(APIView):
    def post(self,request):
        data = request.data
        stu_ser = SutendsSweializer(data=data)
        stu_ser.is_valid()
        stu_ser.save()
        return Response({'msg':'添加成功','code':200})
    def get(self,request):
        id = request.query_params.get('id')
        tea = Teacher.objects.get(id=id)
        print(tea)

        stu_f = Student.objects.filter(teacher=None)
        # print(4)
        stu_t = Student.objects.exclude(teacher=None)
        # print(2)
        stu_fser = SutendsSweializer(stu_f, many=True)
        # print(5)

        stu_tser = SutendsSweializer(stu_t,many=True)
        # print(3)

        return Response({'list_true':stu_tser.data,'list_false':stu_fser.data})

        # return Response({'list_false':stu_fser.data})
    def delete(self,request):
        id = request.query_params.get('id')
        Student.objects.get(id=id).delete()
        return Response({'msg':'删除成功','code':200})
    def put(self,request):
        id = request.data.get('id')
        desc = request.data.get('desc')
        a = Student.objects.get(id=id)
        a.stu_desc=desc
        a.save()
        return Response({'msg':'修改成功','code':200})






class Get(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        stars_id = request.query_params.get('id')
        stars = Stars.objects.get(stul=stars_id)
        stars.stars+=1
        stars.save()
        a = Student.objects.get(id=id)
        return Response({'desc':a.stu_desc,'one':a})


class StudendP(APIView):
    def get(self,request):
        s_id = request.query_params.get('s_id')
        t_id = request.query_params.get('t_id')
        tea = Teacher.objects.get(id=t_id)
        stu = Student.objects.get(id=s_id)
        stu.teacher=tea
        stu.save()
        return Response({'msg':'添加成功','code':200})





